package com.mbc.web.common.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.BranchChangeLog;
import com.mbc.common.entities.User;
import com.mbc.common.param.CriteriaParams;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.control.GridLabelColumn;
import com.mbc.web.common.client.ui.control.GridView;
import com.mbc.web.common.client.ui.control.MTextBox;
import com.mbc.web.common.client.ui.control.Pager;
import com.mbc.web.common.shared.HttpStatus;
import com.mbc.web.common.shared.dto.BranchChangeLogDataListDTO;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;

public class SearchUserBranchChange extends AbstractListView<BranchChangeLog> {

	@UiField GridView gvDisplay;
	@UiField Button btnSearch;
	@UiField Pager pager;
	@UiField MTextBox txtKeyword;
	@UiField Label lblUserName;
	
	private GridLabelColumn colDate = new GridLabelColumn("Date");
	private GridLabelColumn colBranchCode = new GridLabelColumn("Branch Code");
	private GridLabelColumn colBranchName = new GridLabelColumn("Branch Name");
	
	private DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("dd/MM/yyyy");
	
	private static SearchUserBranchChangeUiBinder uiBinder = GWT
			.create(SearchUserBranchChangeUiBinder.class);

	interface SearchUserBranchChangeUiBinder extends
			UiBinder<Widget, SearchUserBranchChange> {
	}

	public SearchUserBranchChange() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}
	
	private CriteriaParams prepareCriteria(){
		CriteriaParams l_cri = new CriteriaParams();
		l_cri.setKeyword(txtKeyword.getText());
		l_cri.setIndex(pager.index);
		l_cri.setSize(pager.size);
		return l_cri;
	}

	@Override
	protected void initialize() {
		pager.setVisible(false);
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			
			@Override
			public void execute() {
				txtKeyword.setFocus(true);
			}
		});
		
		colDate.width = "120px";
		
		gvDisplay.addColumn(colDate);
		gvDisplay.addColumn(colBranchCode);
		gvDisplay.addColumn(colBranchName);
	}

	@Override
	protected void bindDisplay(List<BranchChangeLog> list) {
		gvDisplay.removeAllRows();
		for (BranchChangeLog l_log : list) {
			gvDisplay.addRow(new String[]{
				dateTimeFormat.format(l_log.getChangedDate()),
				l_log.getBranchCode(),
				l_log.getBranchName()
			});
		}
	}

	@Override
	protected void search() {
		if(ViewUtils.isValidate()){
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findUserBranchChangeLogs(prepareCriteria(), findUserBranchChangeLogsCallback());
		}
	}

	@UiHandler("btnSearch")
	void onBtnSearchClick(ClickEvent event) {
		ViewUtils.closeMessageBox();
		final SearchUser l_SearchUser = new SearchUser("User List");
		l_SearchUser.showDialog(new CloseHandler<Modal>() {
			
			@Override
			public void onClose(CloseEvent<Modal> event) {
				if(l_SearchUser.getSelectedEntity() !=  null){
					txtKeyword.setText(l_SearchUser.getSelectedEntity().getLoginId());
					txtKeyword.setFocus(true);
					lblUserName.setText(l_SearchUser.getSelectedEntity().getUserName());
					pager.index = 0;
					search();
				}
			}
		});
	}
	
	private AsyncCallback<BranchChangeLogDataListDTO> findUserBranchChangeLogsCallback(){
		return new AsyncCallback<BranchChangeLogDataListDTO>() {
			
			@Override
			public void onSuccess(BranchChangeLogDataListDTO result) {
				ViewUtils.stopAjaxLoader();
				if(result.getHttpStatus() == HttpStatus.OK){
					dataList = result.getDataList().getEntityList();
					pager.totalRecord = result.getDataList().getTotalRecord();
					bindDisplay(dataList);
					pager.prepareVisible();
				} else if (result.getHttpStatus() == HttpStatus.NOT_FOUND){
					ViewUtils.showErrorMessage(CommonMessage.record_not_found);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		};
	}
	
	@UiHandler("txtKeyword")
	void onTxtKeywordKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
			ViewUtils.closeMessageBox();
			CommonControllerMgr.findUserByLoginId(txtKeyword.getText(), findUserByLoginIdCallback());
		}
	}
	
	private AsyncCallback<User> findUserByLoginIdCallback(){
		return new AsyncCallback<User>() {
			
			@Override
			public void onSuccess(User result) {
				if(result != null){
					lblUserName.setText(result.getUserName());
					pager.index = 0;
					search();
				}
				else
				{
					ViewUtils.showErrorMessage(CommonMessage.record_not_found);
					txtKeyword.setFocus(true);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		};
	}
}
